package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysAppClause;
import com.ruoyi.system.domain.SysAppClausePrescription;
import com.ruoyi.system.service.ISysAppClausePrescriptionService;
import com.ruoyi.system.service.ISysAppClauseService;
import com.ruoyi.web.controller.form.ClausePrescriptionForm;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 条文信息Controller
 * 
 * @author ruoyi
 * @date 2020-09-02
 */
@Controller
@RequestMapping("/system/clause")
public class SysAppClauseController extends BaseController
{
    private String prefix = "system/clause";

    @Autowired
    private ISysAppClauseService sysAppClauseService;
    
    @Autowired
    private ISysAppClausePrescriptionService sysAppClausePrescriptionService;

    @RequiresPermissions("system:clause:view")
    @GetMapping()
    public String clause()
    {
        return prefix + "/clause";
    }

    /**
     * 查询条文信息列表
     */
    @RequiresPermissions("system:clause:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysAppClause sysAppClause)
    {
        startPage();
        List<SysAppClause> list = sysAppClauseService.selectSysAppClauseList(sysAppClause);
        return getDataTable(list);
    }

    /**
     * 导出条文信息列表
     */
    @RequiresPermissions("system:clause:export")
    @Log(title = "条文信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysAppClause sysAppClause)
    {
        List<SysAppClause> list = sysAppClauseService.selectSysAppClauseList(sysAppClause);
        ExcelUtil<SysAppClause> util = new ExcelUtil<SysAppClause>(SysAppClause.class);
        return util.exportExcel(list, "clause");
    }

    /**
     * 新增条文信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存条文信息
     */
    @RequiresPermissions("system:clause:add")
    @Log(title = "条文信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysAppClause sysAppClause)
    {
        return toAjax(sysAppClauseService.insertSysAppClause(sysAppClause));
    }

    /**
     * 修改条文信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysAppClause sysAppClause = sysAppClauseService.selectSysAppClauseById(id);
        mmap.put("sysAppClause", sysAppClause);
        return prefix + "/edit";
    }
    
    

    /**
     * 修改保存条文信息
     */
    @RequiresPermissions("system:clause:edit")
    @Log(title = "条文信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysAppClause sysAppClause)
    {
        return toAjax(sysAppClauseService.updateSysAppClause(sysAppClause));
    }

    /**
     * 删除条文信息
     */
    @RequiresPermissions("system:clause:remove")
    @Log(title = "条文信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysAppClauseService.deleteSysAppClauseByIds(ids));
    }
    
    /**
     * 添加方剂信息
     */
    @GetMapping("/goPrescription/{id}")
    public String goPrescription(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysAppClause sysAppClause = sysAppClauseService.selectSysAppClauseById(id);
        mmap.put("sysAppClause", sysAppClause);
        return prefix + "/addPrescription";
    }
    
    /**
     * 新增方剂信息
     */
    @PostMapping( "/savePrescription")
    @ResponseBody
    public AjaxResult savePrescription(ClausePrescriptionForm form)
    {
    	SysAppClausePrescription sysAppClausePrescription = new SysAppClausePrescription();
    	sysAppClausePrescription.setClauseId(form.getClauseId());
    	sysAppClausePrescription.setPrescriptionId(form.getPrescriptionId());
    	sysAppClausePrescription.setCreateTime(DateUtils.getNowDate());
        return toAjax(sysAppClausePrescriptionService.insertSysAppClausePrescription(sysAppClausePrescription));
    }
    
    /**
     * 删除方剂和条文的关系
     * @param form
     * @return
     */
    @PostMapping( "/deletePrescription")
    @ResponseBody
    public AjaxResult deletePrescription(ClausePrescriptionForm form)
    {
    	SysAppClausePrescription sysAppClausePrescription = new SysAppClausePrescription();
    	sysAppClausePrescription.setClauseId(form.getClauseId());
    	sysAppClausePrescription.setPrescriptionId(form.getPrescriptionId());
        return toAjax(sysAppClausePrescriptionService.deletePrescriptionByClauseIdAndPrescriptionId(sysAppClausePrescription));
    }
    
    @PostMapping( "/selectPrescriptionByPrescriptionId")
    @ResponseBody
    public TableDataInfo selectPrescriptionByPrescriptionId(ClausePrescriptionForm form)
    {
    	SysAppClausePrescription sysAppClausePrescription = new SysAppClausePrescription();
    	sysAppClausePrescription.setClauseId(form.getClauseId());
    	sysAppClausePrescription.setPrescriptionId(form.getPrescriptionId());
    	List<SysAppClausePrescription> list = sysAppClausePrescriptionService.selectSysAppClausePrescriptionList(sysAppClausePrescription);
        return getDataTable(list);
    }
}
